// pages/addPay/addPay.js
var baseConfig = require("../../baseConfig")
Page({
  data: {
    isCompany: 0,
    fwxyClick: false,
    fwxy: false,
    second: 60,
    getCaptcha: true,
    yzm: '',
    name: '',
    idCard: '',
    bank: '',
    zfb: '',
    number: '',
    phone: '',
    active: 0,
    selected: true,
    selected1: false,
    select: false,
    tihuoWay: '自提',
    hidden: true,
    xxx: 0,
    //可以通过hidden是否掩藏弹出框的属性，来指定那个弹出框
    array: ['招商银行', '中国建设银行', '中国工商银行', '中国农业银行', '中国光大银行', '兴业银行', '中国民生银行', '中国交通银行', '中国邮政储蓄银行', '中信银行', '广发银行', '平安银行', '中国银行', '华夏银行'],
    index: 0,
    items: [{
        name: '0',
        value: '个体账户'
      },
      {
        name: '1',
        value: '企业账户',
        checked: 'true'
      },
    ]
  },
  onLoad: function (options) {
    console.log("options111111:", options)
    if (options.updateInfo == '1') {
      console.log("更新信息");
      this.setData({
        updateInfo: options.updateInfo
      });
    }
    this.get_bank_alipay_list()
  },
  onReady: function () {
    this.popup = this.selectComponent("#popup");
  },
  getCaptcha() { // 点击获取验证码
    let that = this;
    console.log('点击获取验证码');
    let name = this.data.name
    let idCard = this.data.idCard
    let number = this.data.number
    let phone = this.data.phone
    let nameReg = /^([\u4e00-\u9fa5]{1,20}|[a-zA-Z\.\s]{1,20})$/; // 姓名正则
    let phoneReg = /^1[3456789]\d{9}$/; // 手机号正则
    let idReg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/; // 身份证正则
    // let yhkReg = /^([1-9]{1})(\d{14}|\d{18})$/; // 银行卡正则
    console.log(this.data.name);
    console.log(decodeURI(this.data.name));
    console.log(encodeURI(this.data.name));
    if (name == '') {
      wx.showToast({
        title: '请输入姓名'
      })
      return false
    } else if (!(nameReg.test(this.data.name))) {
      wx.showToast({
        title: '请按格式填写姓名',
        duration: 2000,
        icon: 'none'
      });
      return false
    } else if (idCard == '') {
      wx.showToast({
        title: '请输入身份证号码'
      })
      return false
    } else if (!(idReg.test(this.data.idCard))) {
      wx.showToast({
        title: '身份证号码有误',
        duration: 2000,
        icon: 'none'
      });
      return false;
    } else if (number == '') {
      wx.showToast({
        title: '请输入银行卡号'
      })
      return false
    } 
    // else if (!(yhkReg.test(this.data.number))) {
    //   wx.showToast({
    //     title: '银行卡账号有误',
    //     duration: 2000,
    //     icon: 'none'
    //   });
    //   return false;
    // } 
    else if (phone == '') {
      wx.showToast({
        title: '请输入手机号'
      })
      return false
    } else if (!(phoneReg.test(this.data.phone))) {
      wx.showToast({
        title: '手机号格式错误',
        icon: 'none'
      });
      return false;
    } else {
      if (phoneReg.test(this.data.phone)) {
        this.timer();
        this.setData({
          getCaptcha: false,
        });
        wx.request({
          url: baseConfig.baseUrl + `get_gong_mao_verify_code`,
          method: 'GET',
          data: {
            name: decodeURI(this.data.name),
            phone: this.data.phone,
            IDCardNumber: this.data.idCard,
            bankCardNum: this.data.number,
            updateInfo: this.data.updateInfo,
          },
          header: {
            'content-type': 'application/json',
            "UID": wx.getStorageSync("userid")
          },
          success: (res) => {
            console.log(res.data.data);
            if (res.data.data.status == '500') {
              let msg = res.data.data.message + ',请仔细校对输入信息!';
              this.showMsg(msg);
              this.setData({
                getCaptcha: true
              })
            } else {
              console.log('验证码正确发送');
            }
          }
        });
      }
    }
  },
  timer: function () { // 倒计时
    let promise = new Promise((resolve, reject) => {
      let setTimer = setInterval(() => {
        this.setData({
          second: this.data.second - 1
        })
        if (this.data.second <= 0) {
          this.setData({
            getCaptcha: true
          })
          resolve(setTimer)
        }
      }, 1000)
    })
    promise.then((setTimer) => {
      clearInterval(setTimer)
    })
  },
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      isCompany: e.detail.value
    })
  },
  radioChange1: function (e) {
    if (this.data.fwxy == true) {
      this.setData({
        fwxy: false
      })
    } else if (this.data.fwxy == false) {
      this.setData({
        fwxy: true
      })
    }
    console.log('radio发生change事件，携带value值为：', this.data.fwxy)
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value, e)
    this.setData({
      index: e.detail.value,
      bank: e.currentTarget.dataset.bank
    })
  },
  bindShowMsg() {
    this.setData({
      select: !this.data.select
    })
  },
  modalinput(e) { // 点击提交按钮(银行卡)
    let name = this.data.name
    let idCard = this.data.idCard
    let number = this.data.number
    let phone = this.data.phone
    let fwxy = this.data.fwxy
    let nameReg = /^([\u4e00-\u9fa5]{1,20}|[a-zA-Z\.\s]{1,20})$/; // 姓名正则
    let phoneReg = /^1[3456789]\d{9}$/; // 手机号正则
    let idReg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/; // 身份证正则
    // let yhkReg = /^([1-9]{1})(\d{14}|\d{18})$/; // 银行卡正则
    console.log(fwxy, this.data.name, this.data.idCard, this.data.phone, this.data.number, this.data.array[this.data.index]);
    if (name == '') {
      wx.showToast({
        title: '请输入姓名'
      })
      return false
    } else if (!(nameReg.test(this.data.name))) {
      wx.showToast({
        title: '请按格式填写姓名',
        duration: 2000,
        icon: 'none'
      });
      return false
    } else if (idCard == '') {
      wx.showToast({
        title: '请输入身份证号码'
      })
      return false
    } else if (!(idReg.test(this.data.idCard))) {
      wx.showToast({
        title: '身份证号码有误',
        duration: 2000,
        icon: 'none'
      });
      return false;
    } else if (number == '') {
      wx.showToast({
        title: '请输入银行卡号'
      })
      return false
    }
    // else if (!(yhkReg.test(this.data.number))) {
    //   wx.showToast({
    //     title: '银行卡账号有误',
    //     duration: 2000,
    //     icon: 'none'
    //   });
    //   return false;
    // }
    else if (phone == '') {
      wx.showToast({
        title: '请输入手机号'
      })
      return false
    } else if (!(phoneReg.test(this.data.phone))) {
      wx.showToast({
        title: '手机号格式错误',
        icon: 'none'
      });
      return false;
    }else if (fwxy == false) {
      wx.showToast({
        title: '请勾选同意服务协议'
      })
      return false
    }
    this.setData({
      hidden: false,
      accountType: 1,
    })
  },
  //确认提交按钮
  confirmClick: function () {
    this.setData({
      hidden: true,
    })
    if (this.data.isCompany == undefined) {
      this.setData({
        isCompany: 0
      })
    }
    this.getyhkData()
  },
  fwxyClick() {
    console.log('111fwxyClick1');
    // this.setData({
    //   fwxyClick: true
    // })
    wx.navigateTo({
      url: '/pages/serviceAgreements/serviceAgreements',
    })
  },
  // 实时显示
  yzmblur: function (e) {
    console.log(e.detail.value);
    this.setData({
      yzm: e.detail.value
    })
  },
  nameblur: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  idCardblur: function (e) {
    console.log(e.detail.value);
    this.setData({
      idCard: e.detail.value
    })
  },
  numberblur: function (e) {
    this.setData({
      number: e.detail.value
    })
  },
  zfbblur: function (e) {
    this.setData({
      zfb: e.detail.value
    })
  },
  phoneblur: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  //取消按钮
  cancel: function () {
    console.log('取消确认提交');
    this.setData({
      hidden: true,
      fwxyClick: false
    });
  },
  selected: function (e) {
    this.setData({
      selected1: false,
      selected: true,
      xxx: 0
    })
  },
  selected1: function (e) {
    this.setData({
      selected: false,
      selected1: true,
      xxx: 1
    })
  },
  getyhkData() {
    var uid = wx.getStorageSync("userid")
    console.log(uid, this.data.name, this.data.idCard, this.data.phone, this.data.array[this.data.index], this.data.number, 'selected', this.data.accountType, this.data.updateInfo);
    if (uid != undefined) {
      wx.request({
        url: baseConfig.url + 'tuiwaApp/update_bank_alipay',
        method: 'POST',
        data: {
          isCompany: this.data.isCompany,
          uid: uid,
          trueName: this.data.name,
          IDCardNumber: this.data.idCard,
          bankName: this.data.array[this.data.index],
          bankCardNum: this.data.number,
          aliPayAccount: '',
          phone: this.data.phone,
          accountType: this.data.accountType,
          captcha: this.data.yzm,
          updateInfo: this.data.updateInfo
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log(res.data.data);
          if (res.data.data !== '银行卡与银行预留手机号不匹配或银行卡信息有误') {
            wx.showModal({
              title: '您已提交成功，等待人工审核，我们会尽快确认！',
              confirmText: "知道了",
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  //点击确定按钮
                  console.log('进入我的银行卡/支付宝');
                  // let xxx = this.data.xxx
                  wx.navigateTo({
                    url: "/pages/myPayCard/myPayCard",
                  })
                } else if (res.cancel) {
                  //点击取消按钮
                }
              }
            })
          } else {
            wx.showToast({
              title: '银行卡信息有误',
              icon: 'error'
            })
          }
        }
      })
    }
  },
  showPopup() {
    this.popup.showPopup();
  },
  showMsg: function (n) { // 轻提示方法
    wx.showToast({
      title: n,
      icon: "none"
    });
  },
  get_bank_alipay_list() {
    let that = this
    var loginUserId = wx.getStorageSync("openid")
    wx.request({
      url: baseConfig.url + 'tuiwaApp/get_bank_alipay_list',
      method: 'POST',
      data: {
        loginUserId: loginUserId,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res.data.data[0]);
        if (!res.data.data[0].bankName) {
          console.log('111');
          that.setData({
            aliPayAccount1: res.data.data[0].aliPayAccount,
          })
          console.log(that.data.aliPayAccount1);
        }
        that.setData({
          uid: res.data.data[0].uid,
          name: res.data.data[0].trueName,
          phone: res.data.data[0].phone,
          idCard: res.data.data[0].IDCardNumber,
        })
      }
    })
  },
})